import java.util.ArrayList;

import it.randomtower.engine.World;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

public class GUIWorld extends World {
	TiledMap map;
	ArrayList<GUICharacter> players = new ArrayList<GUICharacter>();
	int characterToMove;
	GUIGUI menu;
	
	boolean menuState = true;
	boolean moveState = false;
	

	public GUIWorld(int id, GameContainer container) throws SlickException {
		super(id, container);
	}

	public void init(GameContainer container, StateBasedGame game)
			throws SlickException {
		super.init(container, game);

		// add the robots
		// used to be in the TwoCharacters() method
		// create the two robots
		players.add(new GUICharacter(0, 0));
		add(players.get(0));
		//players.get(0).setHight(32);
		//players.get(0).setWidth(32);

		players.add(new GUICharacter(32, 32));
		add(players.get(1));
		//players.get(1).setHight(32);
		//players.get(1).setWidth(32);

		
		// setup background
		map = new TiledMap("images/simplelevel.tmx");
		loadEntityFromMap(map);
		menu = new GUIGUI(10, 10);
		
		menuState = true;
		add(menu);
		menu.setIsShown(menuState);
	}

	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		// draw the map
		for (int l = 0; l < map.getLayerCount(); l++) {
			String value = map.getLayerProperty(l, "type", null);
			if (value == null || !value.equalsIgnoreCase("entity")) {
				for (int w = 0; w < map.getWidth(); w++) {
					for (int h = 0; h < map.getHeight(); h++) {
						Image img = map.getTileImage(w, h, l);
						if (img != null) {
							g.drawImage(img, w * img.getWidth(),
									h * img.getHeight());
							
						}
					}
				}
			}
		}
		
		if(menu.isShown()){
			menu.render(g, 250, 250);
			menu.setXAndYForButtons(250, 250);
		}

		super.render(container, game, g);
	}

	public boolean isAPlayerAtLocation(int x, int y) {
		for (int i = 0; i <= players.size() - 1; i++) {
			players.get(i).setSelected(false);
			if (players.get(i).isPlayerAtLocation(x, y)) {
				//characterToMove = i;
				return true;
			}
		}
		return false;
	}
	
	
	public void update(GameContainer container, StateBasedGame game, int delta)
		throws SlickException {
		Input input = container.getInput();
		int mouseX = input.getMouseX();
		int mouseY = input.getMouseY();
		
		boolean mousePressed = false;
		if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
			mousePressed = true;
			System.out.println("Mouse Pressed");
		}
		if (mousePressed) {
			if (menuState) {

				if (menu.update(input)) {
					menuState = false;
					moveState = true;
					players.get(characterToMove).setSelected(true);
				}

			}

			else if (moveState) {
				//players.get(characterToMove).setSelected(true);
				boolean mouseOnCharacter = false;
				for (GUICharacter c : players) {
					if (c.isPlayerAtLocation(mouseX, mouseY))
						mouseOnCharacter = true;
				}
				
				if(mouseOnCharacter)
					System.out.println("Mouse is on Character");
				
				
				if (!mouseOnCharacter) {
					System.out.println("Move Character");
					players.get(characterToMove).movePlayer(mouseX, mouseY);
					moveState = false;
					menuState = true;
					players.get(characterToMove).setSelected(false);
					if((characterToMove += 1) >= players.size())
						characterToMove = 0;
				}
			}
		}
		
		super.update(container, game, delta);
		/**
		 * Make it so that when you enter this there are "mini" states
		 * one for knowing if someone is selected, one for knowing what was clicked
		 * in the gui.
		 * Send the work to the unit that needs to do the movement and such
		 * don't let the main world do all the work.
		 * 
		 * Different States:
		 * Option not selected
		 * Move Selected
		 * Attack Selected
		 * 
		 */
	}
/*
	public void update(GameContainer container, StateBasedGame game, int delta)
			throws SlickException {
		Input input = container.getInput();
		int mouseX = input.getMouseX();
		int mouseY = input.getMouseY();
		mouseX = (mouseX / 16) * 16;
		mouseY = (mouseY / 16) * 16;
		boolean mousePressed = false;
		
		if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)
				&& mousePressed == false) {
			mousePressed = true;
		}

		if (mousePressed) {

			if (isAPlayerAtLocation(mouseX, mouseY)) {
				players.get(characterToMove).setSelected(true);

			} else {
				if (characterToMove < 0) {
					// Do nothing
				}

				else {
					players.get(characterToMove).movePlayer(mouseX, mouseY);
					characterToMove = -1;
				}
			}
		}
		super.update(container, game, delta);

	}*/

}
